﻿using System.Collections.Generic;
using System.Linq;
using InvoiceInvoker.Logic.Repositories;

namespace InvoiceInvoker.Logic
{
	public partial class Invoice
	{
		public enum InvoiceStatus { Created, Paid }

		public void UpdateStatus(InvoiceStatus status)
		{
			Invoice invoice = new Invoice();
			invoice.CreationDate = CreationDate;
			invoice.CustomerId = CustomerId;
			invoice.Id = Id;
			invoice.LeftToPay = LeftToPay;
			invoice.Number = Number;
			invoice.Paid = Paid;
			invoice.PaymentCurrency = PaymentCurrency;
			invoice.PaymentDeadline = PaymentDeadline;
			invoice.PaymentType = PaymentType;
			invoice.Products = Products;
			invoice.RegisteredSellerId = RegisteredSellerId;
			invoice.Remarks = Remarks;
			invoice.SaleDate = SaleDate;
			invoice.SellerId = SellerId;
			invoice.ToPay = ToPay;
			invoice.ToPayInWords = ToPayInWords;

			invoice.Status = status.ToString();

			new InvoiceRepository(RegisteredSellerId.Value).Update(invoice);
		}

		public List<string> GetVatRates()
		{
			List<string> vatRates = VatRatesProvider.GetVatRates();
			List<string> result = new List<string>();

			foreach (string vatRate in vatRates)
			{
				if (Products.Any(p => p.VatRate == vatRate))
					result.Add(vatRate);
			}

			return result;
		}

		public decimal GetGeneralNetValue()
		{
			return Products.Sum(p => p.NetValue).Value;
		}

		public decimal GetGeneralNetValue(string vatRate)
		{
			return Products.Where(p => p.VatRate == vatRate).Sum(p => p.NetValue).Value;
		}

		public decimal GetGeneralVatValue()
		{
			return Products.Sum(p => p.VatValue).Value;
		}

		public decimal GetGeneralVatValue(string vatRate)
		{
			return Products.Where(p => p.VatRate == vatRate).Sum(p => p.VatValue).Value;
		}

		public decimal GetGeneralGrossValue()
		{
			return Products.Sum(p => p.GrossValue).Value;
		}

		public decimal GetGeneralGrossValue(string vatRate)
		{
			return Products.Where(p => p.VatRate == vatRate).Sum(p => p.GrossValue).Value;
		}
	}
}
